package access.spg.android.com.accessibilitytest;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.content.Intent;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import java.util.List;
import java.util.Random;

@RequiresApi(api = Build.VERSION_CODES.O)
public class ReadNovelService extends AccessibilityService {
    private static String tag="SmallVideoService";

    private static boolean start=false;

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d(tag,"onServiceConnected");
//        searchNode();
    }

    @Override
    public void onInterrupt() {
        Log.d(tag,"onInterrupt");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(tag,"onUnbind");
        return super.onUnbind(intent);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
//        Log.d(tag,"动动手"+event.getEventType());
        switch (event.getEventType())
        {
            case AccessibilityEvent.TYPE_VIEW_SCROLLED:
                scrollLeft(false);
                break;
        }
    }






    /**
     * 向上翻页
     *
     *
     * */
    private void scrollLeft(boolean loop)
    {
        if(!start)
        {
            start=true;
        }else
        {
            if(!loop)
                return;
        }

        int count=0;
        while (count<5000)
        {
            try {//滞留
                Random ra =new Random();
                int tmp;
                tmp=ra.nextInt(5)+5;
                Log.e(tag, "============scrollUp wait "+tmp+"s===============" );
                Thread.sleep(tmp*1000);

                Toast.makeText(this,"时间："+tmp,Toast.LENGTH_SHORT).show();

                GestureDescription.Builder builder = new GestureDescription.Builder();
                Path path = new Path();
                float x1 = MyUtil.getWindowWidth(this)*0.90f;
                float x2 = MyUtil.getWindowWidth(this)*0.2f;
                float y =MyUtil.getWindowHeight(this)*0.85f;
                path.moveTo(x1, y);
                path.lineTo(x2, y);
                GestureDescription gestureDescription = builder
                        .addStroke(new GestureDescription.StrokeDescription(path, 200L, 800L,false))
                        .build();

                dispatchGesture(gestureDescription, new GestureResultCallback() {
                    @Override
                    public void onCompleted(GestureDescription gestureDescription) {
                        super.onCompleted(gestureDescription);
                        Log.e(tag, "============Swipe onCompleted===============" );
                    }

                    @Override
                    public void onCancelled(GestureDescription gestureDescription) {
                        super.onCancelled(gestureDescription);
                        Log.e(tag, "============Swipe onCancelled===============");
                    }
                }, new Handler(Looper.getMainLooper()));

//                scrollLeft(true);
                count++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }



}
